package org.dlow.router;

import org.dlow.common.URL;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @description:
 * @Author: 山沐与山
 * @CreateTime: 2024-05-16 00:19
 */
public class RoundRobinServiceSelector implements ServiceSelector{

    private static AtomicInteger roundRobinId = new AtomicInteger(0);


    @Override
    public URL selectService(List<URL> candidates) {

        roundRobinId.addAndGet(1);
        if (roundRobinId.get() == Integer.MAX_VALUE){
            roundRobinId.set(0);
        }
        return candidates.get(roundRobinId.get() % candidates.size());
    }
}
